package com.bjsj.pro.tools;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bjsj.pro.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import com.bjsj.pro.view.GlideCircleTransform;
import com.bjsj.pro.view.GlideRoundTransform;


/**
 * Created by zhu on 16/9/9.
 * 加载图片的工具
 */
public class GlideManager
{
    private Context mContext;
    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String FOREWARD_SLASH = "/";
    public GlideManager(Context context) {
        this.mContext = context;
    }
    // 将资源ID转为Uri
    public Uri resourceIdToUri(int resourceId) {
        return Uri.parse(ANDROID_RESOURCE + mContext.getPackageName() + FOREWARD_SLASH + resourceId);
    }
//    // 加载网络图片
//    public void loadUrlImage(String url, ImageView imageView, Activity activity) {
//        /**activity.isFinishing()防止activity被销毁了  网咯请求里面有glide请求  但glide还去加载和显示图片，就会报异常*/
//        if(!activity.isFinishing()){
//            Glide.with(mContext)
//                    .load(url).placeholder(R.mipmap.default_image)
//                    .error(R.mipmap.default_image)
////                .placeholder(R.drawable.default_usehead)
////                    .error(R.mipmap.ic_winstyle_delete)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存源资源和转换后的资源
//                    .crossFade()
//                    .into(imageView);
//            /**all:缓存源资源和转换后的资源
//             none:不作任何磁盘缓存
//             source:缓存源资源
//             result：缓存转换后的资源*/
//        }
//    }
    // 加载网络图片
    public void loadUrlImage(String url, ImageView imageView) {
        /**activity.isFinishing()防止activity被销毁了  网咯请求里面有glide请求  但glide还去加载和显示图片，就会报异常*/
//        Glide.with(mContext).load(url)
////                .placeholder(R.drawable.pic_zhanwei_mid)
//                .error(R.drawable.pic_zhanwei_mid)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
//                .crossFade()//淡入淡出效果
//                .into(imageView);
//

        Glide.with(mContext).load(url)
                .asBitmap()
                .centerCrop()
                .placeholder(R.drawable.pic_zhanwei_mid)
                .error(R.drawable.pic_zhanwei_mid)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .into(imageView);
            /**all:缓存源资源和转换后的资源
             none:不作任何磁盘缓存
             source:缓存源资源
             result：缓存转换后的资源*/
        }
    // 加载网络图片
    public void loadUrlImageres(String url, ImageView imageView,int res) {
        /**activity.isFinishing()防止activity被销毁了  网咯请求里面有glide请求  但glide还去加载和显示图片，就会报异常*/
        Glide.with(mContext).load(url)
                .asBitmap()
                .centerCrop().placeholder(res)
                .error(res)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .into(imageView);
        /**all:缓存源资源和转换后的资源
         none:不作任何磁盘缓存
         source:缓存源资源
         result：缓存转换后的资源*/
    }
    // 加载drawable图片
    public void loadResImage(int resId, ImageView imageView) {
        Glide.with(mContext)
                .load(resourceIdToUri(resId))
                .placeholder(R.drawable.pic_zhanwei_mid)
                .error(R.drawable.pic_zhanwei_mid)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .crossFade()
                .into(imageView);
    }
    // 加载本地图片
    public void loadLocalImage(String path, ImageView imageView) {
        Glide.with(mContext)
                .load("file://" + path)
                .placeholder(R.drawable.pic_zhanwei_mid)
                .error(R.drawable.pic_zhanwei_mid)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
//                .crossFade()
                .into(imageView);
    }

    // 加载本地图片
    public void loadLocalImagerex(String path, ImageView imageView,int res) {
        Glide.with(mContext)
                .load("file://" + path)
                .placeholder(R.drawable.pic_zhanwei_mid)
                .error(R.drawable.pic_zhanwei_mid)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .crossFade()
                .into(imageView);
    }
    // 加载网络圆型图片
    public void loadCircleImage(String url, ImageView imageView) {
        Glide.with(mContext)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .error(R.drawable.user_pic_portrait)
                .crossFade()
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }
    // 加载drawable圆型图片
    public void loadCircleResImage(int resId, ImageView imageView) {
        Glide.with(mContext)
                .load(resourceIdToUri(resId))
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .crossFade()
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }
    // 加载本地圆型图片
    public void loadCircleLocalImage(String path, ImageView imageView) {
        Glide.with(mContext)
                .load("file://" + path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .crossFade()
                .transform(new GlideCircleTransform(mContext))
                .error(R.drawable.user_pic_portrait)
                .into(imageView);
    }
    //加载gif动态图
    public void loadGifImage(String url, ImageView imageView) {
        Glide.with(mContext)
                .load(url)
                .asGif()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .crossFade()
                .into(imageView);
    }

    // 加载网络圆角图片
    public void loadCircleJImage(String url, ImageView imageView) {
        Glide.with(mContext)
                .load(url)
//                .asBitmap()
//                .format(DecodeFormat.PREFER_ARGB_8888)
                .placeholder(R.drawable.pic_zhanwei_mid)
                .error(R.drawable.pic_zhanwei_mid)
                . transform(new CenterCrop(mContext), new GlideRoundTransform(mContext,5)).into(imageView);
    }
    //清除缓存
    public void clearMemory(Context context){
        Glide.get(context).clearMemory();
    }
}
